﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using Microsoft.Practices.Unity;

using ePlanet.ArchiveBorrow.Models;
using ePlanet.ArchiveBorrow.Services;
using Web.SystemConfig.Services;
using ePlanet.ArchiveMange.DataAccess;
using ePlanet.ArchiveMange.Services;
using ePlanet.ArchiveMange.Enum;
using DreamBuilder.Core.Container;
using Web.SystemConfig.WorkFlow;
using System.Collections;

namespace Web.Views.ArchiveBorrow
{
    public partial class PrintLineDownArchiveBorrowList : System.Web.UI.Page
    {

        [Dependency]
        public IArchiveMenuService ArchiveMenuService
        {
            get;
            set;
        }

        protected DataTable ArchiveDataTable
        {
            get;
            set;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ArchiveDataTable = new DataTable();
                ArchiveDataTable.Columns.Add("ARCHIVENUM", typeof(string));
                ArchiveDataTable.Columns.Add("TITLE", typeof(string));
                ArchiveDataTable.Columns.Add("ARCHIVESOURCE", typeof(string));
                ArchiveDataTable.Columns.Add("FILEORGID", typeof(string));

                if (string.IsNullOrEmpty(Request.QueryString["BorrowDataType"]))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "noargumentResult", "<script>alert('缺少档案类型信息！');</script>");
                    return;
                }
                Web.Views.ArchiveBorrow.ArchiveBorrowApply.PrintModel oPrintModel = Session["PrintModel"] as Web.Views.ArchiveBorrow.ArchiveBorrowApply.PrintModel;

                if (oPrintModel == null || oPrintModel.DataList.Count == 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "noDataResult", "<script>alert('请先选择要借阅的档案！');</script>");
                    return;
                }

                //绑定从页面传递来的值
                this.lblBorrowReason.Text = oPrintModel.BorrowReason;
                //this.lblBorrowDate.Text = oPrintModel.ExpridTime;
                this.lblBorrowDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                this.lblIDCard.Text = oPrintModel.IDCard;
                this.lblPhoneNumber.Text = oPrintModel.TelNum;
                this.lblUserName.Text = oPrintModel.UserName;
                this.lblDepartment.Text = oPrintModel.UserOrg;

                foreach (string str in oPrintModel.ArchiveType)
                {
                    foreach (ListItem item in this.cblARCHIVETYPE.Items)
                    {
                        if (string.Equals(item.Value, str))
                        {
                            item.Selected = true;
                        }
                    }
                }
                foreach (string str in oPrintModel.UserType)
                {
                    foreach (ListItem item in this.cblUSERTYPE.Items)
                    {
                        if (string.Equals(item.Value, str))
                        {
                            item.Selected = true;
                            if (string.Equals(str, "3"))
                            {
                                item.Text = "复制__" + oPrintModel.CopyPage + "__页";
                            }
                        }
                    }
                }

                List<string> dataIdList = oPrintModel.DataList;
                try
                {
                    //绑定档案条目
                    int baseTypeId = ArchiveMenuService.GetArchiveBaseIDById(int.Parse(Request.QueryString["BorrowDataType"]));

                    switch (baseTypeId)
                    {
                        //文书类
                        case ((int)ArchiveBaseType.Document):
                            IDocumentArchiveService documentArchiveService = UContainer.Resolve<IDocumentArchiveService>();
                            ArchiveDataTable = documentArchiveService.GetDocumentArchiveDataByIds(dataIdList);
                            break;
                        //会计类
                        case ((int)ArchiveBaseType.Account):
                            IAccountArchiveService accountArchiveService = UContainer.Resolve<IAccountArchiveService>();
                            ArchiveDataTable = accountArchiveService.GetAccountArchiveDataByIds(dataIdList);
                            break;
                        //基建类
                        case ((int)ArchiveBaseType.BulidProject):
                            IBuildProjectArchiveService buildProjectService = UContainer.Resolve<IBuildProjectArchiveService>();
                            ArchiveDataTable = buildProjectService.GetBuildProjectArchiveDataByIds(dataIdList);
                            break;
                        //设备类
                        case ((int)ArchiveBaseType.Equipment):
                            IEquipmentService equipService = UContainer.Resolve<IEquipmentService>();
                            ArchiveDataTable = equipService.GetEquipmentArchiveDataByIds(dataIdList);
                            break;
                        //油气勘探类
                        case ((int)ArchiveBaseType.OliSearch):
                            IOilSearchArchiveService oliSearchService = UContainer.Resolve<IOilSearchArchiveService>();
                            ArchiveDataTable = oliSearchService.GetOilSearchArchiveDataByIds(dataIdList);
                            break;
                        //音像类
                        case ((int)ArchiveBaseType.AudioVedio):
                            IAudioArchiveService audioService = UContainer.Resolve<IAudioArchiveService>();
                            ArchiveDataTable = audioService.GetAudioArchiveDataByIds(dataIdList);
                            break;
                        //实物类
                        case ((int)ArchiveBaseType.RealKind):
                            IRealKindArchiveService realService = UContainer.Resolve<IRealKindArchiveService>();
                            ArchiveDataTable = realService.GetRealKindArchiveDataByIds(dataIdList);
                            break;
                        //科研类
                        case ((int)ArchiveBaseType.Technology):
                            ITechArchiveService techService = UContainer.Resolve<ITechArchiveService>();
                            ArchiveDataTable = techService.GetTechArchiveDataByIds(dataIdList);
                            break;
                        //光盘类
                        case ((int)ArchiveBaseType.CDKind):
                            ICDArchiveService cdService = UContainer.Resolve<ICDArchiveService>();
                            ArchiveDataTable = cdService.GetCDArchiveDataByIds(dataIdList);
                            break;
                    }

                    while (ArchiveDataTable.Rows.Count < 10)
                    {
                        ArchiveDataTable.Rows.Add(ArchiveDataTable.NewRow());
                    }
                }
                catch (System.Exception ex)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "exceptionResult", "<script>alert('生成借阅单发生异常！');</script>");
                }
            }
        }


    }
}